﻿using Expression_Test.ExpressionGeneric.ExpressionAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Expression_Test.ExpressionGeneric
{
    /// <summary>
    /// 表达式目录树转换扩展类
    /// </summary>
    public static class ExpressionGenericMapperExtend
    {
        /// <summary>
        /// 获取 属性名称
        /// </summary>
        /// <returns></returns>
        public static string GetMappingName(this PropertyInfo propertyInfo)
        {
            //是否标记属性
            if (propertyInfo.IsDefined(typeof(MappingNameAttribute), true))
            {
                var display = propertyInfo.GetCustomAttribute(typeof(MappingNameAttribute));

                if ((display as MappingNameAttribute) is null)
                {
                    return "";
                }

                return (display as MappingNameAttribute)?.mappingName ?? "";
            }

            //返回本身属性名
            return propertyInfo.Name;
        }
        /// <summary>
        /// 获取 字段名称
        /// </summary>
        /// <returns></returns>
        public static string GetMappingName(this FieldInfo fieldInfo)
        {
            //是否标记属性
            if (fieldInfo.IsDefined(typeof(MappingNameAttribute), true))
            {
                var display = fieldInfo.GetCustomAttribute(typeof(MappingNameAttribute));

                if ((display as MappingNameAttribute) is null)
                {
                    return "";
                }

                return (display as MappingNameAttribute)?.mappingName ?? "";
            }

            //返回本身属性名
            return fieldInfo.Name;
        }
    }
}
